<?php

namespace wopan\lib\channel;

use wopan\lib\WoPanAES;
use wopan\lib\WoPanChannel;

class Wohome extends WoPanChannel
{
    public static $channel = 'wohome';

    public $spaceType = '';

    // 空間類型, 0 個人云， 1 家庭云
    public function setSpaceType($spaceType)
    {
        $this->spaceType = $spaceType;
    }

    public function verifySetPwd($psToken = "")
    {
        $response = $this->request("VerifySetPwd", [
            "clientId" => $this->clientId,
            "param"   => WoPanAES::encrypt([
                "psToken" => $psToken,
                "clientId" => $this->clientId,
            ], $this->accessToken),
        ]);
        $response->data = WoPanAES::decrypt($response->data, $this->accessToken);
        $response->data = json_decode($response->data, true);
        return $response;
    }

    public function deleteRecycleData($pageNo = 1, $pageSize = 20, $sortRule = "0")
    {
        $response = $this->request("DeleteRecycleData", [
            "clientId" => $this->clientId,
            "param"   => WoPanAES::encrypt([
                "pageNo" => $pageNo,
                "pageSize" => $pageSize,
                "sortRule" => $sortRule,
                "clientId" => $this->clientId,
            ], $this->accessToken),
        ]);
        $response->data = WoPanAES::decrypt($response->data,  $this->accessToken);
        return $response;
    }
    // todo 這裏的pageNo必須從0開始
    public function queryRecycleData($pageNo = 1, $pageSize = 20, $sortRule = "0")
    {
        $response = $this->request("QueryRecycleData", [
            "clientId" => $this->clientId,
            "param"   => WoPanAES::encrypt([
                "pageNo" => $pageNo,
                "pageSize" => $pageSize,
                "sortRule" => $sortRule,
                "clientId" => $this->clientId,
            ], $this->accessToken),
        ]);
        $response->data = WoPanAES::decrypt($response->data,  $this->accessToken);
        $response->data = json_decode($response->data, true);
        return $response;
    }

    public function queryUploadLog($pageNo = 0, $pageSize = 20, $sortType = 1, $orderType = 1)
    {
        $response = $this->request("QueryUploadLog", [
            "clientId" => $this->clientId,
            "param"   => WoPanAES::encrypt([
                "orderType" => $orderType,
                "pageNo" => $pageNo,
                "pageSize" => $pageSize,
                "sortType" => $sortType,
                "clientId" => $this->clientId,
            ], $this->accessToken),
        ]);
        $response->data = WoPanAES::decrypt($response->data,  $this->accessToken);
        $response->data = json_decode($response->data, true);
        return $response;
    }

    public function fCloudProductPackage($vipLevel = "0")
    {
        return $this->request("FCloudProductPackage", [
            "clientId" => $this->clientId,
            "param"   => [
                "clientId" => $this->clientId,
                "vipLevel" => $vipLevel
            ]
        ]);
    }

    public function queryMuid()
    {

        $response = $this->request("QueryMuid", [
            "clientId" => $this->clientId
        ]);
        // todo 这里的解密有问题
        // var_dump($response);
        $response->data['muid'] = WoPanAES::decrypt($response->data['muid'], $this->clientSecret);
        return $response;
    }

    public function familyUserCurrent()
    {
        $response = $this->request("FamilyUserCurrent", [
            "clientId" => $this->clientId
        ]);
        return $response;
    }

    public function queryAllFiles($spaceType = 0, $parentDirectoryId = 0, $pageNum = 0, $pageSize = 0, $sortRule = 0)
    {
        $response = $this->request("QueryAllFiles", [
            "param"    => WoPanAES::encrypt([
                'spaceType' => $spaceType,
                'parentDirectoryId' => $parentDirectoryId,
                'pageNum' => $pageNum,
                'pageSize' => $pageSize,
                'sortRule' => $sortRule,
                "clientId" => $this->clientId
            ], $this->accessToken),
            "secret"   => true
        ]);
        $response->data = WoPanAES::decrypt($response->data,  $this->accessToken);
        $response->data = json_decode($response->data, true);
        return $response;
    }

    public function getDownloadUrl($fidList = [], $spaceType = 0)
    {
        $response = $this->request("GetDownloadUrl", [
            "param"    => WoPanAES::encrypt([
                'fidList' => $fidList,
                'spaceType' => $spaceType,
                "clientId" => $this->clientId
            ], $this->accessToken),
            "secret"   => true
        ]);
        $response->data = WoPanAES::decrypt($response->data,  $this->accessToken);
        return $response;
    }

    public function queryCloudUsageInfo($phoneNum)
    {
        $response = $this->request("QueryCloudUsageInfo", [
            "param"    => WoPanAES::encrypt([
                'phoneNum' => $phoneNum,
                "clientId" => $this->clientId,
                'secret' => true
            ], $this->accessToken),
            "secret"   => true
        ]);
        $response->data = WoPanAES::decrypt($response->data,  $this->accessToken);
        $response->data = json_decode($response->data, true);
        return $response;
    }
    // fileType 1  照片
    // fileType 2  視頻
    // fileType 3  音樂
    // fileType 4  文檔
    // fileType 5  其他
    public function queryTypeFiles($spaceType = 0, $fileType = 1, $pageNum = 0, $pageSize = 50, $psToken = null, $isMergeSysFile = 0)
    {
        $response = $this->request("QueryTypeFiles", [
            "param"    => WoPanAES::encrypt([
                'spaceType' => $spaceType,
                "fileType" => $fileType,
                'pageNum' => $pageNum,
                'pageSize' => $pageSize,
                'psToken' => $psToken,
                'isMergeSysFile' => $isMergeSysFile,
                "clientId" => $this->clientId,
            ], $this->accessToken),
            "secret"   => true
        ]);
        $response->data = WoPanAES::decrypt($response->data,  $this->accessToken);
        $response->data = json_decode($response->data, true);
        return $response;
    }

    public function createDirectory($directoryName, $spaceType = 0, $familyId = "3466541", $parentDirectoryId = 0)
    {
        $response = $this->request("CreateDirectory", [
            "param"    => WoPanAES::encrypt([
                'spaceType' => $spaceType,
                "familyId" => $familyId,
                'parentDirectoryId' => $parentDirectoryId,
                'directoryName' => $directoryName,
                "clientId" => $this->clientId,
            ], $this->accessToken),
            "secret"   => true
        ]);
        $response->data = WoPanAES::decrypt($response->data,  $this->accessToken);
        $response->data = json_decode($response->data, true);
        return $response;
    }

    public function deleteFile($dirList = [], $fileList = [], $spaceType = 0, $vipLevel = "1")
    {
        $response = $this->request("DeleteFile", [
            "param"    => WoPanAES::encrypt([
                'spaceType' => $spaceType,
                "vipLevel" => $vipLevel,
                'dirList' => $dirList,
                'fileList' => $fileList,
                "clientId" => $this->clientId,
            ], $this->accessToken),
            "secret"   => true
        ]);
        $response->data = WoPanAES::decrypt($response->data,  $this->accessToken);
        return $response;
    }

    public function renameFileOrDirectory($id, $name, $type = 1, $fileType = 5, $spaceType = 0)
    {
        $response = $this->request("RenameFileOrDirectory", [
            "param"    => WoPanAES::encrypt([
                'spaceType' => $spaceType,
                "id" => $id,
                'name' => $name,
                'type' => $type,
                'fileType' => $fileType,
                "clientId" => $this->clientId,
            ], $this->accessToken),
            "secret"   => true
        ]);
        $response->data = WoPanAES::decrypt($response->data,  $this->accessToken);
        return $response;
    }

    public function moveFile($targetDirId, $targetType = 0, $dirList = 1, $fileList = 5, $spaceType = 0)
    {
        $response = $this->request("MoveFile", [
            "param"    => WoPanAES::encrypt([
                'spaceType' => $spaceType,
                'targetDirId' => $targetDirId,
                "targetType" => $targetType,
                'dirList' => $dirList,
                'fileList' => $fileList,
                'secret' => false,
                "clientId" => $this->clientId,
            ], $this->accessToken),
            "secret"   => true
        ]);
        $response->data = WoPanAES::decrypt($response->data,  $this->accessToken);
        return $response;
    }
    public function searchFile($keyWord, $pageNo = 1, $pageSize = 5, $searchType = 2)
    {
        $response = $this->request("SearchFile", [
            "param"    => WoPanAES::encrypt([
                'keyWord' => $keyWord,
                'pageNo' => $pageNo,
                "pageSize" => $pageSize,
                'searchType' => $searchType,
                "clientId" => $this->clientId,
            ], $this->accessToken),
            "secret"   => true
        ]);
        $response->data = WoPanAES::decrypt($response->data,  $this->accessToken);
        $response->data = json_decode($response->data, true);
        return $response;
    }

    public function shareFile(array $fileIds = [], array  $fileFolderIds = [], $days = 99999)
    {
        $response = $this->request("ShareFile", [
            "param"    => WoPanAES::encrypt([
                'fileIds' => implode(",", $fileIds),
                'fileFolderIds' => implode(",", $fileFolderIds),
                "days" => $days,
                "clientId" => $this->clientId,
            ], $this->accessToken),
            "secret"   => true
        ]);
        $response->data = WoPanAES::decrypt($response->data,  $this->accessToken);
        $response->data = json_decode($response->data, true);
        return $response;
    }
    // operType 為1刪除分享鏈接，為2刪除歷史記錄
    public function CancelOrDelShareFile(array $shareId, $operType = 1)
    {
        $response = $this->request("CancelOrDelShareFile", [
            "param"    => WoPanAES::encrypt([
                'operType' => $operType,
                'shareId' => implode(",", $shareId),
                "clientId" => $this->clientId,
            ], $this->accessToken),
            "secret"   => true
        ]);
        $response->data && $response->data = WoPanAES::decrypt($response->data,  $this->accessToken);
        return $response;
    }
    public function reductionRecycleData(array $deleteNos)
    {
        $response = $this->request("ReductionRecycleData", [
            "param"    => WoPanAES::encrypt([
                'deleteNos' => implode(",", $deleteNos),
                "clientId" => $this->clientId,
            ], $this->accessToken),
            "secret"   => true
        ]);
        $response->data && $response->data = WoPanAES::decrypt($response->data,  $this->accessToken);
        return $response;
    }
    
}
